<?php
$auth = Auth::getAuth('current_user');
$db = new Database();
$userCompany = new userQueries();
$company = $userCompany->getCompany($auth['company_id']);
$report_id = $_GET['id'];
$form_id = $_GET['form_id'];

$newForm = new Form($db, $form_id);
$reportDoc = new Report($db, $report_id);
?>
<script type="text/javascript" src="/js/functions/report/report.js"></script>

<div class="content_wrapper">
    <div class="padding_5">
        <div class="body_header_container">
            <?php
            // Report header
            $header = $newForm->form_name . ' Report ';
            echo functions::workspace_header($header, "report");
            ?>
        </div>
        <div class="workspace report_ws" style="background: none;position: relative;padding:10px;height: 1800px">
            <!-- Node -->
            <div class="fields_below">
                <div class="label_below">Parameters: </div>
                <input type="text" class="display" id="active_fields" value="<?php echo $newForm->active_fields; ?>">
                <input type="button" class="btn-blueBtn" action="append_parameter" value="Add Parameter">
                <div class="input_position_below" style="margin-top: 5px;">
                    <div style="height: 500px; width: 100%; border: 1px Solid #ccc;overflow: auto">
                        <table data="data-parameter" class="table_data display_data dataTable">
                            <tr>
                                <th>Label</th>
                                <th>FieldName</th>
                                <th>Operator</th>
                                <th>Action</th>
                            </tr>
                            <?php
                            if ($reportDoc->id == 0) {
                                //new report
                                ?>
                                <tr data-type="tr_parameters">
                                    <td><input type="text" column="label" class="form-text"/></td>
                                    <td>
                                        <select column="field" class="form-select">
                                            <?php
                                            $fields = explode(",", $newForm->active_fields);
                                            foreach ($fields as $key => $value) {
                                                ?>
                                                <option  value="<?php echo $value; ?>">
                                                    <?php
                                                    echo $value;
                                                    ?>
                                                </option>
                                                <?php
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <td>
                                        <select column="operator" class="form-select">
                                            <option value="=">Equal (==)</option>
                                            <option value="<=">Less than equal (<=)</option>
                                            <option value=">=">Greater than equal (>=)</option>
                                            <option value=">">Greater than(>)</option>
                                            <option value="<">Less than (<)</option>
                                            <option value="!=">Not equal(!=)</option>
                                            <option value="%">Contains(%)</option>
                                        </select>
                                    </td>
                                    <td>
                                        <div class="icon-trash cursor deleteParameter" style="margin-left: auto;margin-right: auto" data-type="deactivate" data-user-id="1"></div>
                                    </td>
                                </tr>   
                                <?php
                            } else {
                                $parameter_arr = json_decode($reportDoc->parameters, true);
                                foreach ($parameter_arr as $parameter) {
                                    ?>
                                    <tr data-type="tr_parameters">
                                        <td><input type="text" column="label" value="<?php echo $parameter['Column']; ?>" class="form-text"/></td>
                                        <td>
                                            <select column="field" class="form-select">
                                                <?php
                                                $fields = explode(",", $newForm->active_fields);
                                                foreach ($fields as $key => $value) {
                                                    ?>
                                                    <option  value="<?php echo $value; ?>"
                                                    <?php
                                                    if ($parameter['Field'] == $value) {
                                                        echo 'selected';
                                                    }
                                                    ?>
                                                             >
                                                                 <?php
                                                                 echo $value;
                                                                 ?>
                                                    </option>
                                                    <?php
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td>
                                            <select column="operator" class="form-select">
                                                <?php
                                                if ($parameter['Operator'] == '=') {
                                                    ?>
                                                    <option value="=" selected>Equal (==)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value="=">Equal (==)</option>
                                                    <?php
                                                }

                                                if ($parameter['Operator'] == '<=') {
                                                    ?>
                                                    <option value="<=" selected>Less than equal (<=)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value="<=">Less than equal (<=)</option>
                                                    <?php
                                                }

                                                if ($parameter['Operator'] == '>=') {
                                                    ?>
                                                    <option value=">=" selected>Greater than equal (>=)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value=">=">Greater than equal (>=)</option>
                                                    <?php
                                                }

                                                if ($parameter['Operator'] == '>') {
                                                    ?>
                                                    <option value=">" selected>Greater than(>)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value=">">Greater than(>)</option>
                                                    <?php
                                                }

                                                if ($parameter['Operator'] == '<') {
                                                    ?>
                                                    <option value="<" selected>Less than (<)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value="<">Less than (<)</option>
                                                    <?php
                                                }

                                                if ($parameter['Operator'] == '!=') {
                                                    ?>
                                                    <option value="!=" selected>Not equal(!=)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value="!=">Not equal(!=)</option>
                                                    <?php
                                                }

                                                if ($parameter['Operator'] == '%') {
                                                    ?>
                                                    <option value="%" selected>Contains(%)</option>
                                                    <?php
                                                } else {
                                                    ?>
                                                    <option value="%">Contains(%)</option>
                                                    <?php
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td>
                                            <div class="icon-trash cursor deleteParameter" style="margin-left: auto;margin-right: auto" data-type="deactivate" data-user-id="1"></div>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                            ?>
                        </table>
                    </div>
                </div>
            </div>
            <div class="fields_below">
                <div class="label_below">Columns: </div>
                <input type="button" class="btn-blueBtn" action="add_column" value="Add Column">      
                <label style="font-style: italic;color: #ff0000;">You can sort column headers via drag and drop.</label>
                <div class="input_position_below" style="margin-top: 5px">
                    <div style="height: 500px; width: 100%; border: 1px Solid #ccc;overflow: auto">
                        <table id="report_columns" class="table_data display_data dataTable" width="400px">
                            <tr class="sortableRow">
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <div class="fields_below">
                <?php
                if ($reportDoc->id == 0) {
                    $y_axis_max = 10;
                    $y_axis_min = 0;
                    $y_axis_interval = 1;
                    $symbol = '';
                } else {
                    $y_axis_max = $reportDoc->yaxis_max;
                    $y_axis_min = $reportDoc->yaxis_min;
                    $y_axis_interval = $reportDoc->yaxis_interval;
                    $symbol = json_decode($reportDoc->symbol, true);
                }
                ?>
                <div class="label_below">Chart Settings: </div>
                <div class="fields">
                    <div class="label_basic"> Y-Axis Max: </div>
                    <div class="input_position">
                        <input type="number" class="form-text" value="<?php echo $y_axis_max; ?>" name="yAxisMax" id="yAxisMax" placeholder="Y-Axis Max"/>
                    </div>
                </div>
                <div class="fields">
                    <div class="label_basic"> Y-Axis Max: </div>
                    <div class="input_position">
                        <input type="number" class="form-text" value="<?php echo $y_axis_min; ?>"  name="yAxisMin" id="yAxisMin" placeholder="Y-Axis Min"/>
                    </div>
                </div>
                <div class="fields">
                    <div class="label_basic"> Y-Axis Interval: </div>
                    <div class="input_position">
                        <input type="number" class="form-text" value="<?php echo $y_axis_interval; ?>"  name="yAxisInterval" id="yAxisInterval" placeholder="Y-Axis Interval"/>
                    </div>
                </div>
                <div class="fields">
                    <div class="label_below2">Symbol:</div>
                    <div class="input_position_below">
                        <?php if ($reportDoc->id == 0) { ?>
                            <label><input id="symbol_static" data-properties-type="symbol_static" class="" value="static" type="radio" checked="checked" name="chart_symbol">Static </label>
                            <label><input id="symbol_computed" data-properties-type="symbol_computed" class="" value="computed" type="radio" name="chart_symbol">Computed </label>
                            <?php
                        } else {
                            if ($symbol["ValueType"] == "static") {
                                ?>
                                <label><input id="symbol_static" data-properties-type="symbol_static" class="" value="static" type="radio" checked="checked" name="chart_symbol">Static </label>
                                <label><input id="symbol_computed" data-properties-type="symbol_computed" class="" value="computed" type="radio" name="chart_symbol">Computed </label>
                                <?php
                            } else {
                                ?>
                                <label><input id="symbol_static" data-properties-type="symbol_static" class="" value="static" type="radio"  name="chart_symbol">Static </label>
                                <label><input id="symbol_computed" data-properties-type="symbol_computed" class="" value="computed" type="radio" checked="checked" name="chart_symbol">Computed </label>
                                <?php
                            }
                        }
                        ?>
                        <textarea data-type="textbox" class="form-textarea symbol_value" id="symbol_value" data-properties-type="defaultValue" data-object-id="1" style="resize:vertical;" value=""><?php echo $symbol["Value"]; ?></textarea>
                    </div>
                </div>
                <input type="button" class="btn-blueBtn" style="margin-top: 5px" action="add_plot_bond" value="Add Plot Band"> 
                <div class="input_position_below" style="margin-top: 5px">
                    <div style="height: 500px; width: 100%; border: 1px Solid #ccc;overflow: auto">
                        <table id="report_chartPlotBands" class="table_data display_data dataTable" width="400px">
                            <tr>
                                <th>Name</th>
                                <th>From Type</th>
                                <th>From</th>
                                <th>To Type</th>
                                <th>To</th>
                                <th>Color</th>
                                <th>Action</th>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <input id="report_id" class="display" type="text" value="<?php echo $report_id ?>" />
        <input id="form_id" class="display" type="text" value="<?php echo $form_id ?>" />
        <input id="parameters" class="display" type="text" value='<?php echo $reportDoc->parameters ?>' />
        <input id="columns" class="display" type="text" value='<?php echo $reportDoc->columns ?>' />
        <div id="column-data" class="display">
            <?php echo $reportDoc->columns ?>
        </div>
        <div id="plot-bands-data" class="display">
            <?php echo $reportDoc->plotbands ?>
        </div>
    </div>